package org.apache.hadoop.hdfs.server.datanode;

import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Date;

public class PrototypeUtilities {
	
	public static String getHostname() {

			try {
				return InetAddress.getLocalHost().getHostName();
			} catch (UnknownHostException e) {
				e.printStackTrace();
				return "unknown";
			}
	}
	
  /**
   * Converts an IOExcpetion (not subclasses) to SocketException.
   * This is typically done to indicate to upper layers that the error 
   * was a socket error rather than often more serious exceptions like 
   * disk errors.
   */
  public static IOException ioeToSocketException(IOException ioe) {
    if (ioe.getClass().equals(IOException.class)) {
      // "se" could be a new class in stead of SocketException.
      IOException se = new SocketException("Original Exception : " + ioe);
      se.initCause(ioe);
      /* Change the stacktrace so that original trace is not truncated
       * when printed.*/ 
      se.setStackTrace(ioe.getStackTrace());
      return se;
    }
    // otherwise just return the same exception.
    return ioe;
  }
  
  public static String getPreambleString(Class caller) {
	  return "";
	  //return "M801-X977594X " + PrototypeUtilities.getHostname() + " " + caller.getSimpleName() + " " + (new Date(System.currentTimeMillis())) + " ";
  }
  
  public static String getBitwiseRepresentation(long number) {
	  StringBuilder builder = new StringBuilder();
	  
	  for (int i = 0; i < 64; i++) {
		  if ((number & 1L) == 1L) { builder.append('1'); }
		  else { builder.append('0'); }
		  number = number >> 1;
	  }
	  
	  return builder.reverse().toString();
  }
  
  public static int BITS_IN_A_LONG = 64;
  
  public static final int HASH_DIVISION_CONSTANT = 2000;
  
  public static final String CYCLICAL_BLOCK_LIST_FILENAME = "/data/hdfs/cyclical_blocks.txt";
  
  public static final int MAX_LENGTH_OF_CHUNK_QUEUES = 20;
  
  public static boolean DETAILED_DEBUG_MODE = PrototypeUtilities.SUMMARY_DEBUG_MODE && false;
  
  public static boolean ANNOUNCE_METHOD_NAME = false;
  
  public static boolean SUMMARY_DEBUG_MODE = false;
  
  public static long CYCLICAL_BLOCK_LIST_REFRESH_PERIOD = 20000;
  
  public static void sleep(long time) {
	  try {
		  Thread.sleep(time);
	  }
	  catch (InterruptedException ex) {
		  ex.printStackTrace();
	  }
  }
  
  public static void main(String[] args) {

	  

  }
	
}